﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Fluent;
using PefStudio.Services;
using PefStudio.StartUp;

namespace PefStudio.Windows
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    [Export(Bootstrapper.MainWindowContract, typeof(Window))]
    public partial class MainWindow : RibbonWindow, IPartImportsSatisfiedNotification
    {
        public static CompositionContainer CompositionContainer = Bootstrapper.Container;
        public const string RibbonTabsContract = "PefStudio.MainWindow.RibbonTabItems";
        public const string RibbonToolBarsContract = "PefStudio.MainWindow.Ribbon.ToolBarItems";
        public const string RibbonMenusContract = "PefStudio.MainWindow.Ribbon.Menu";
        public const string RibbonQuickAccessItemsContract = "PefStudio.MainWindow.Ribbon.QuickAccessItems";
        public const string MainToolBarsContract = "PefStudio.MainWindow.ToolBars";
        public const string MainStatusBarContract = "PefStudio.MainWindow.StatusBar";
        public const string LoadedCommandsContract = "PefStudio.MainWindow.LoadedCommands";
        public const string ClosingCommandsContract = "PefStudio.MainWindow.ClosingCommands";
        public const string ClosedCommandsContract = "PefStudio.MainWindow.ClosedCommands";

        [ImportMany(RibbonTabsContract, typeof(IRibbonTabItem), AllowRecomposition = true)]
        private IEnumerable<IRibbonTabItem> RibbonTabs { get; set; }

        [ImportMany(RibbonTabsContract, typeof(IRibbonContextualTabGroup), AllowRecomposition = true)]
        private IEnumerable<IRibbonContextualTabGroup> RibbonContextualGroups { get; set; }

        [ImportMany(RibbonToolBarsContract, typeof(IRibbonChildItem), AllowRecomposition = true)]
        private IEnumerable<IRibbonChildItem> RibbonToolBars { get; set; }

        [Import(RibbonMenusContract, typeof(IRibbonBackstage), AllowRecomposition = true)]
        private IRibbonBackstage RibbonMenu { get; set; }

        [ImportMany(RibbonQuickAccessItemsContract, typeof(IRibbonChildItem), AllowRecomposition = true)]
        private IEnumerable<IRibbonChildItem> RibbonQuickAccessItems { get; set; }

        [ImportMany(RibbonQuickAccessItemsContract, typeof(IRibbonQuickAccessMenuItem), AllowRecomposition = true)]
        private IEnumerable<IRibbonQuickAccessMenuItem> RibbonQuickAccessMenuItems { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            Ribbon.AutomaticStateManagement = false;
        }

        public void OnImportsSatisfied()
        {
            LoggingService.Debug("OnImportsSatisfied");

            CreateService.CreateCollection(Ribbon.Tabs, RibbonTabs);
            CreateService.CreateCollection(Ribbon.ContextualGroups, RibbonContextualGroups);
            CreateService.CreateCollection(Ribbon.ToolBarItems, RibbonToolBars);

            if (RibbonMenu != null)
                Ribbon.Menu = RibbonMenu as Backstage;

            CreateService.CreateCollection(Ribbon.QuickAccessItems, RibbonQuickAccessMenuItems);
            foreach (var item in RibbonQuickAccessItems)
            {
                var quickAccessMenuItem = new QuickAccessMenuItem {Target = item as Control};
                Ribbon.QuickAccessItems.Add(quickAccessMenuItem);
            }
        }
    }
}
